home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / misc / bgui12.lha / demos / colorwheel.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  28KB  |  384 lines

  1. ;/* Execute me to compile with DICE V3.0
  2. dcc colorwheel.c -proto -mi -ms -mRR -3.0 -lbgui
  3. quit
  4. */
  5. /*
  6.  *      COLORWHEEL.C
  7.  *
  8.  *      (C) Copyright 1995 Jaba Development.
  9.  *      (C) Copyright 1995 Jan van den Baard.
  10.  *          All Rights Reserved.
  11.  */
  12.  
  13. /*
  14.  *      This example is based on the original ColorWheel.c
  15.  *      code by Commodore.
  16.  */
  17.  
  18. #include "democode.h"
  19.  
  20. #include <dos/dos.h>
  21. #include <gadgets/colorwheel.h>
  22. #include <gadgets/gradientslider.h>
  23. #include <graphics/displayinfo.h>
  24.  
  25. #include <clib/colorwheel_protos.h>
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. /*
  31. **      Library base pointers.
  32. **/
  33. struct Library *ColorWheelBase = NULL, *GradientSliderBase = NULL;
  34.  
  35. /*
  36. **      Object ID's
  37. **/
  38. #define ID_QUIT                 1
  39. #define ID_WHEEL                2
  40.  
  41. /*
  42. **      Structure for LoadRGB32().
  43. **/
  44. struct load32 {
  45.         UWORD           l32_len;
  46.         UWORD           l32_pen;
  47.         ULONG           l32_red;
  48.         ULONG           l32_grn;
  49.         ULONG           l32_blu;
  50. };
  51.  
  52. /*
  53. **      Ensure correct operation on ECS.
  54. **/
  55. #define GRADCOLORS 4
  56.  
  57. /*
  58. **      The program even does version checking... sjeez...
  59. **/
  60. extern struct Library *SysBase;
  61.  
  62. /*
  63. **      And were in...
  64. **/
  65. VOID StartDemo( void )
  66. {
  67.         Object                  *WN_Window, *GA_Quit, *GA_Wheel, *GA_GrSlider, *GA_Master, *RealSlider = NULL;
  68.         struct Window           *win;
  69.         struct Screen           *scr;
  70.         ULONG                   winsig = 0L, sigrec, rc;
  71.         BOOL                    running = TRUE;
  72.         ULONG                   colortable[ 96 ];
  73.         struct load32           color_list[ GRADCOLORS + 1 ];
  74.         WORD                    penns[ GRADCOLORS + 1 ];
  75.         struct ColorWheelRGB    rgb;
  76.         struct ColorWheelHSB    hsb;
  77.         ULONG                   modeID = HIRES_KEY;
  78.         UWORD                   numPens, i;
  79.         DisplayInfoHandle       displayhandle;
  80.         struct DimensionInfo    dimensioninfo;
  81.  
  82.         /*
  83.         **      Need OS 3.0.
  84.         **/
  85.         if ( SysBase->lib_Version < 39 ) {
  86.                 Tell( "OS 3.0 required!" );
  87.                 exit( 0 );
  88.         }
  89.  
  90.         /*
  91.         **      Open the libraries. Get display information etc...
  92.         **/
  93.         if ( ColorWheelBase = OpenLibrary( "gadgets/colorwheel.gadget", 39L )) {
  94.                 if ( GradientSliderBase = OpenLibrary( "gadgets/gradientslider.gadget", 39L )) {
  95.                         if ( displayhandle = FindDisplayInfo( modeID )) {
  96.                                 if ( GetDisplayInfoData( displayhandle, ( UBYTE * )&dimensioninfo, sizeof( struct DimensionInfo ), DTAG_DIMS, NULL )) {
  97.                                         /*
  98.                                         **      Open up a screen with as many
  99.                                         **      colors as possible. Also make it
  100.                                         **      like your workbench screen.
  101.                                         **/
  102.                                         if ( scr = OpenScreenTags( NULL, SA_Depth,              dimensioninfo.MaxDepth,
  103.                                                                          SA_SharePens,          TRUE,
  104.                                                                          SA_LikeWorkbench,      TRUE,
  105.                                                                          SA_Interleaved,        TRUE,
  106.                                                                          SA_Title,              "ColorWheel Screen",
  107.                                                                          TAG_END )) {
  108.                                                 /*
  109.                                                 **      Get the colors.
  110.                                                 **/
  111.                                                 GetRGB32( scr->ViewPort.ColorMap, 0L, 32L, colortable );
  112.  
  113.                                                 /*
  114.                                                 **      Setup gradient slider
  115.                                                 **      as color 0.
  116.                                                 **/
  117.                                                 rgb.cw_Red   = colortable[ 0 ];
  118.                                                 rgb.cw_Green = colortable[ 1 ];
  119.                                                 rgb.cw_Blue  = colortable[ 2 ];
  120.  
  121.                                                 /*
  122.                                                 **      Convert the RGB values to
  123.                                                 **      HSB values.
  124.                                                 **/
  125.                                                 ConvertRGBToHSB( &rgb, &hsb );
  126.  
  127.                                                 /*
  128.                                                 **      Maximum brightness.
  129.                                                 **/
  130.                                                 hsb.cw_Brightness = 0xffffffff;
  131.  
  132.                                                 numPens = 0;
  133.  
  134.                                                 /*
  135.                                                 **      Build the pen array for the
  136.                                                 **      gradient slider (4 pens).
  137.                                                 **/
  138.                                                 while ( numPens < GRADCOLORS ) {
  139.                                                         /*
  140.                                                         **      Compute dim level of this pen.
  141.                                                         **/
  142.                                                         hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / GRADCOLORS ) * numPens );
  143.                                                         /*
  144.                                                         **      Convert this dim level
  145.                                                         **      to RGB values.
  146.                                                         **/
  147.                                                         ConvertHSBToRGB( &hsb, &rgb );
  148.                                                         /*
  149.                                                         **      Allocate a pen.
  150.                                                         **/
  151.                                                         if (( penns[ numPens ] = ObtainPen( scr->ViewPort.ColorMap, -1, rgb.cw_Red, rgb.cw_Green, rgb.cw_Blue, PEN_EXCLUSIVE )) == -1 )
  152.                                                                 break;
  153.                                                         /*
  154.                                                         **      Stuff it in the color list.
  155.                                                         **/
  156.                                                         color_list[ numPens ].l32_len = 1;
  157.                                                         color_list[ numPens ].l32_pen = penns[ numPens ];
  158.                                                         /*
  159.                                                         **      Next...
  160.                                                         **/
  161.                                                         numPens++;
  162.                                                 }
  163.  
  164.                                                 /*
  165.                                                 **      Terminate pen array and
  166.                                                 **      color list.
  167.                                                 **/
  168.                                                 penns[ numPens ]              = ~0;
  169.                                                 color_list[ numPens ].l32_len = 0;
  170.  
  171.                                                 /*
  172.                                                 **      Create the gradient slider here
  173.                                                 **      _with_ the EXT_NoRebuild tag set
  174.                                                 **      to TRUE. This _must_ be set to
  175.                                                 **      TRUE otherwise the external class
  176.                                                 **      will rebuild the object after each
  177.                                                 **      re-size resulting in colourful
  178.                                                 **      crashes when it gets attached to
  179.                                                 **      the colorwheel :)
  180.                                                 **/
  181.                                                 GA_GrSlider = ExternalObject,
  182.                                                         EXT_MinWidth,           10,
  183.                                                         EXT_MinHeight,          10,
  184.                                                         EXT_ClassID,            "gradientslider.gadget",
  185.                                                         EXT_NoRebuild,          TRUE,
  186.                                                         GRAD_PenArray,          penns,
  187.                                                         PGA_Freedom,            LORIENT_VERT,
  188.                                                         TAG_END );
  189.  
  190.                                                 /*
  191.                                                 **      Pick up a pointer to the
  192.                                                 **      "real" gradient slider object.
  193.                                                 **      This will be the pointer we
  194.                                                 **      pass to the colorwheel.
  195.                                                 **/
  196.                                                 if ( GA_GrSlider )
  197.                                                         GetAttr( EXT_Object, GA_GrSlider, ( ULONG * )&RealSlider );
  198.  
  199.                                                 /*
  200.                                                 **      Create a small window.
  201.                                                 **/
  202.                                                 WN_Window = WindowObject,
  203.                                                         WINDOW_Title,           "ColorWheel",
  204.                                                         WINDOW_RMBTrap,         TRUE,
  205.                                                         WINDOW_Screen,          scr,
  206.                                                         WINDOW_ScaleWidth,      20,
  207.                                                         WINDOW_ScaleHeight,     20,
  208.                                                         WINDOW_AutoAspect,      TRUE,
  209.                                                         /*
  210.                                                         **      The colorwheel is really slow :(
  211.                                                         **/
  212.                                                         WINDOW_NoBufferRP,      TRUE,
  213.                                                         WINDOW_MasterGroup,
  214.                                                                 GA_Master = VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
  215.                                                                         StartMember, TitleSeperator( "Wheel & Slider" ), EndMember,
  216.                                                                         StartMember,
  217.                                                                                 HGroupObject, Spacing( 4 ),
  218.                                                                                         StartMember,
  219.                                                                                                 /*
  220.                                                                                                 **      The EXT_NoRebuild tag may _not_ be set to
  221.                                                                                                 **      TRUE for a colorwheel object. This is due
  222.                                                                                                 **      to the fact that colorwheels cannot change
  223.                                                                                                 **      size. This is also the reason why we need
  224.                                                                                                 **      to track the attributes of the colorwheel.
  225.                                                                                                 **/
  226.                                                                                                 GA_Wheel = ExternalObject,
  227.                                                                                                         EXT_MinWidth,           30,
  228.                                                                                                         EXT_MinHeight,          30,
  229.                                                                                                         EXT_ClassID,            "colorwheel.gadget",
  230.                                                                                                         WHEEL_Screen,           scr,
  231.                                                                                                         /*
  232.                                                                                                         **      Pass a pointer to the "real" gradient slider
  233.                                                                                                         **      here.
  234.                                                                                                         **/
  235.                                                                                                         WHEEL_GradientSlider,   RealSlider,
  236.                                                                                                         WHEEL_Red,              colortable[ 0 ],
  237.                                                                                                         WHEEL_Green,            colortable[ 1 ],
  238.                                                                                                         WHEEL_Blue,             colortable[ 2 ],
  239.                                                                                                         GA_FollowMouse,         TRUE,
  240.                                                                                                         GA_ID,                  ID_WHEEL,
  241.                                                                                                         /*
  242.                                                                                                         **      These attributes of the colorwheel are
  243.                                                                                                         **      tracked and reset to the object after
  244.                                                                                                         **      it has been rebuild. This way the current
  245.                                                                                                         **      colorwheel internals will not be lost
  246.                                                                                                         **      after the object is re-build.
  247.                                                                                                         **/
  248.                                                                                                         EXT_TrackAttr,          WHEEL_Red,
  249.                                                                                                         EXT_TrackAttr,          WHEEL_Green,
  250.                                                                                                         EXT_TrackAttr,          WHEEL_Blue,
  251.                                                                                                         EXT_TrackAttr,          WHEEL_Hue,
  252.                                                                                                         EXT_TrackAttr,          WHEEL_Saturation,
  253.                                                                                                         EXT_TrackAttr,          WHEEL_Brightness,
  254.                                                                                                 EndObject,
  255.                                                                                         EndMember,
  256.                                                                                         /*
  257.                                                                                         **      Add the externalclass object of the
  258.                                                                                         **      gradient slider here. Right next to
  259.                                                                                         **      the colorwheel :)
  260.                                                                                         **/
  261.                                                                                         StartMember,
  262.                                                                                                 GA_GrSlider, FixWidth( 20 ),
  263.                                                                                         EndMember,
  264.                                                                                 EndObject,
  265.                                                                         EndMember,
  266.                                                                         StartMember, HorizSeperator, EndMember,
  267.                                                                         StartMember,
  268.                                                                                 HGroupObject,
  269.                                                                                         VarSpace( DEFAULT_WEIGHT ),
  270.                                                                                         StartMember, GA_Quit = KeyButton( "_Quit", ID_QUIT ), EndMember,
  271.                                                                                         VarSpace( DEFAULT_WEIGHT ),
  272.                                                                                 EndObject, FixMinHeight,
  273.                                                                         EndMember,
  274.                                                                 EndObject,
  275.                                                 EndObject;
  276.  
  277.                                                 if ( WN_Window ) {
  278.                                                         /*
  279.                                                         **      Make button selectable by the keyboard.
  280.                                                         **/
  281.                                                         GadgetKey( WN_Window, GA_Quit, "q" );
  282.                                                         /*
  283.                                                         **      Open up the window.
  284.                                                         **/
  285.                                                         if ( win = WindowOpen( WN_Window )) {
  286.                                                                 /*
  287.                                                                 **      Obtain window sigmask.
  288.                                                                 **/
  289.                                                                 GetAttr( WINDOW_SigMask, WN_Window, &winsig );
  290.                                                                 /*
  291.                                                                 **      Wait for messages.
  292.                                                                 **/
  293.                                                                 do {
  294.                                                                         sigrec = Wait( winsig );
  295.  
  296.                                                                         /*
  297.                                                                         **      Window signal?
  298.                                                                         **/
  299.                                                                         if ( sigrec & winsig ) {
  300.                                                                                 while ( WN_Window && (( rc = HandleEvent( WN_Window )) != WMHI_NOMORE )) {
  301.                                                                                         switch ( rc ) {
  302.  
  303.                                                                                                 case    WMHI_CLOSEWINDOW:
  304.                                                                                                 case    ID_QUIT:
  305.                                                                                                         /*
  306.                                                                                                         **      The end.
  307.                                                                                                         **/
  308.                                                                                                         running = FALSE;
  309.                                                                                                         break;
  310.  
  311.                                                                                                 case    ID_WHEEL:
  312.                                                                                                         /*
  313.                                                                                                         **      Obtain the wheel it's HSB settings.
  314.                                                                                                         **/
  315.                                                                                                         GetAttr( WHEEL_HSB, GA_Wheel, ( ULONG * )&hsb );
  316.  
  317.                                                                                                         i = 0;
  318.  
  319.                                                                                                         /*
  320.                                                                                                         **      Recompute the gradient slider
  321.                                                                                                         **      dim colors for the current
  322.                                                                                                         **      colorwheel setting.
  323.                                                                                                         **/
  324.                                                                                                         while ( i < numPens ) {
  325.                                                                                                                 /*
  326.                                                                                                                 **      Compute dim level of this pen.
  327.                                                                                                                 **/
  328.                                                                                                                 hsb.cw_Brightness = ( ULONG )0xffffffff - ((( ULONG )0xffffffff / numPens ) * i );
  329.                                                                                                                 /*
  330.                                                                                                                 **      Convert to RGB values.
  331.                                                                                                                 **/
  332.                                                                                                                 ConvertHSBToRGB(&hsb,&rgb);
  333.                                                                                                                 /*
  334.                                                                                                                 **      Setup the pens in the color list.
  335.                                                                                                                 **/
  336.                                                                                                                 color_list[ i ].l32_red = rgb.cw_Red;
  337.                                                                                                                 color_list[ i ].l32_grn = rgb.cw_Green;
  338.                                                                                                                 color_list[ i ].l32_blu = rgb.cw_Blue;
  339.                                                                                                                 /*
  340.                                                                                                                 **      Next...
  341.                                                                                                                 **/
  342.                                                                                                                 i++;
  343.                                                                                                         }
  344.                                                                                                         /*
  345.                                                                                                         **      Show the changes in the gradient slider.
  346.                                                                                                         **/
  347.                                                                                                         LoadRGB32( &scr->ViewPort, ( ULONG * )color_list );
  348.                                                                                                         break;
  349.                                                                                         }
  350.                                                                                 }
  351.                                                                         }
  352.                                                                 } while ( running );
  353.                                                         }
  354.                                                         /*
  355.                                                         **      Kill the window.
  356.                                                         **/
  357.                                                         DisposeObject( WN_Window );
  358.                                                 }
  359.                                                 /*
  360.                                                 **      Release the allocated pens.
  361.                                                 **/
  362.                                                 while ( numPens > 0 ) {
  363.                                                         numPens--;
  364.                                                         ReleasePen( scr->ViewPort.ColorMap, penns[ numPens ] );
  365.                                                 }
  366.                                                 /*
  367.                                                 **      Close the screen.
  368.                                                 **/
  369.                                                 CloseScreen( scr );
  370.                                         }
  371.                                 }
  372.                         }
  373.                         /*
  374.                         **      Close the gradient.gadget.
  375.                         **/
  376.                         CloseLibrary( GradientSliderBase );
  377.                 }
  378.                 /*
  379.                 **      Close the colorwheel.gadget.
  380.                 **/
  381.                 CloseLibrary( ColorWheelBase );
  382.         }
  383. }
  384.